package com.zzgc.common.fliter;

import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class CrosFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse)servletResponse;
        HttpServletRequest request = (HttpServletRequest)servletRequest;
//        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Access-Control-Allow-Origin",request.getHeader("Origin"));
        //response.setHeader("Set-Cookie", "HttpOnly;Secure;SameSite=None");
//        response.addHeader("Access-Control-Allow-Methods","GET,POST,DELETE,PUT,PATCH,OPTIONS");
        response.addHeader("Access-Control-Allow-Methods","*");
        //允许跨域请求中携带cookie
        response.addHeader("Access-Control-Allow-Credentials","true");
        response.addHeader("Access-Control-Request-Headers", "Origin, X-Requested-With,cookie,content-Type, Accept, Authorization");
        response.addHeader("Access-Control-Allow-Headers","Content-Type,Origin,X-CAF-Authorization-Token,sessionToken,token,cookie,Content-Disposition");
//        if(((HttpServletRequest)servletRequest).getMethod().equals("OPTIONS")){
//            response.getWriter().println("ok");
//            return;
//        }
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {

    }
}
